const OSS = require('ali-oss');
const path = require('path');
const fs = require('fs');
const Core = require('@alicloud/pop-core');

const moment = require('moment');
require('moment/locale/zh-cn');
moment.locale('zh-cn');
const curDate = moment().format('MM-DD-HH-mm-ss');

const accessKeyId = 'LTAI5tNDdRyTfBsEsZyTy1Lf';
const accessKeySecret = 'JoAdEfWyQhFlSrzrLhRB31PCtgsaJb';

const client = new OSS({
    region: 'oss-cn-shanghai',
    accessKeyId,
    accessKeySecret,
    authorizationV4: false,
    bucket: 'mzydz-manhu',
});

// 自定义请求头
const headers = {
    // 指定Object的存储类型。
    'x-oss-storage-class': 'Standard',
    // 指定Object的访问权限。
    'x-oss-object-acl': 'public-read',

    'Content-Disposition': 'inline',
    'Content-Type': 'png',
    // 设置Object的标签，可同时设置多个标签。
    // 'x-oss-tagging': 'Tag1=1&Tag2=2',
    // 指定PutObject操作时是否覆盖同名目标Object。此处设置为true，表示禁止覆盖同名Object。
    'x-oss-forbid-overwrite': 'false',
    'x-oss-force-download': 'false',
};

const uploadAndDownloadFile = async (inputPath) => {
    try {
        const orgin = `/aiimg/${curDate}.png`
        const res = await client.put(
            orgin,
            // path.normalize(path.join(__dirname, 'screen.png')),
            path.normalize(inputPath),
            { headers }
        );
        console.log(res, 'res=========');

        return Promise.resolve(res.url);

        // await cdnUpdate();
    } catch (error) {
        console.error('发生错误:', error);
        return Promise.reject(error);
    }
};
// uploadAndDownloadFile();
module.exports = {
    uploadAndDownloadFile,
}

